package 操作zookeeper.javaAPI;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * author Veng Su
 * email  1344114844@qq.com
 * date   18-9-16 下午1:19
 */
//这里实现watcher借口是为了方便构造zookeeper对象
public class APIdemo implements Watcher {
    public final static String CONNECTSTRING = "192.168.0.201:2181,192.168.0.203:2181,192.168.0.204:2181";

    public static CountDownLatch countDownLatch = new CountDownLatch(1);//计数器用于同步连接
    public static ZooKeeper zooKeeper;//zookeeper对象
    public static Stat stat = new Stat();//不给的全局，状态会话会丢失，导致执行失败

    public static void main(String[] args) throws Exception {
        APIdemo apIdemo = new APIdemo();

        zooKeeper = new ZooKeeper(CONNECTSTRING, 500, new APIdemo());//创建zookeeper对象

        countDownLatch.await();
        System.out.println(zooKeeper.getState());
//注意我都是写死的节点。便于学习
//        System.out.println(zooKeeper.getData("/suveng",apIdemo,stat));
//        apIdemo.delete();//这里是删除节点，注意要有才能删除
//        apIdemo.get(apIdemo);//这里是获取节点
//        apIdemo.create();//创建节点
//        apIdemo.set();//修改节点
//        Thread.sleep(2000);
//
//        Thread.sleep(1000);
//        zooKeeper.create("/suveng/sds", "dddd".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//        System.out.println(zooKeeper.getChildren("/suveng",true));


//        权限控制部分：ip,digest,world,super
//        zooKeeper.addAuthInfo("digest","root:root".getBytes());

        //第一种方式做权限
        ACL acl=new ACL(ZooDefs.Perms.CREATE, new Id("digest","root:root"));

        List<ACL> acls=new ArrayList<>();
        acls.add(acl);
        zooKeeper.create("/suveng/auth","2122".getBytes(),acls,CreateMode.PERSISTENT);

        //第二种方式做权限
//        zooKeeper.addAuthInfo("digest","root:root".getBytes());

        //创建新的客户端
        APIdemo apIdemo1=new APIdemo();
        ZooKeeper zooKeeper1=new ZooKeeper(CONNECTSTRING,5000,apIdemo1);
        zooKeeper1.getData("/suveng/auth",true,new Stat());

    }


    private void create() {
        //create
        String res = null;
        try {
            res = zooKeeper.create("/suveng", "hello,suwenguang".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("create " + res);
    }

    private String set() throws KeeperException, InterruptedException {
        zooKeeper.setData("/suveng", "dddd".getBytes(), -1);
        return null;

    }

    private void get(APIdemo apIdemo) throws KeeperException, InterruptedException {
        zooKeeper.getData("/suveng", apIdemo, stat);
    }
    private void delete() throws KeeperException, InterruptedException {
        zooKeeper.delete("/suveng",-1);
    }

    //watcher 的实现方法
    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            if (watchedEvent.getType() == Event.EventType.None && watchedEvent.getPath() == null) {
                countDownLatch.countDown();
                System.out.println(watchedEvent.getState());
            } else if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                try {
                    System.out.println("path---->" + watchedEvent.getPath() + "  |data change--->" +
                            zooKeeper.getData(watchedEvent.getPath(), true, stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (watchedEvent.getType() == Event.EventType.NodeCreated) {
                try {
                    System.out.println("path---->" + watchedEvent.getPath() + "  |created--->" +
                            zooKeeper.getData(watchedEvent.getPath(), true, stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
                try {
                    System.out.println("path---->" + watchedEvent.getPath() + "  |data deleted--->" +
                            zooKeeper.getData(watchedEvent.getPath(), true, stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
                try {
                    System.out.println("path---->" + watchedEvent.getPath() + "  |data child changed--->" +
                            zooKeeper.getData(watchedEvent.getPath(), true, stat));
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
